<template>
  <div class="join-couple-container">
    <div class="join-couple-card">
      <div class="join-couple-header">
        <h1 class="join-couple-title">💌 加入情侣关系</h1>
        <p class="join-couple-subtitle">输入TA分享给你的邀请码</p>
      </div>

      <el-form 
        v-if="!joined" 
        :model="joinForm" 
        :rules="rules" 
        ref="joinFormRef" 
        class="join-couple-form"
      >
        <el-form-item prop="inviteCode">
          <el-input
            v-model="joinForm.inviteCode"
            placeholder="请输入6位邀请码"
            size="large"
            prefix-icon="Key"
            maxlength="6"
            show-word-limit
            @input="handleInput"
          />
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            size="large"
            class="join-button"
            :loading="loading"
            @click="handleJoin"
          >
            加入情侣关系
          </el-button>
        </el-form-item>
      </el-form>

      <!-- 加入成功提示 -->
      <div v-else class="success-section">
        <div class="success-icon">🎉</div>
        <h2 class="success-title">加入成功！</h2>
        <p class="success-subtitle">你们的甜蜜记账之旅开始啦</p>
        
        <el-button 
          type="success" 
          size="large" 
          class="go-home-button"
          @click="goHome"
        >
          前往首页
        </el-button>
      </div>

      <div class="join-couple-footer">
        <router-link to="/create-couple" class="link">还没有邀请码？点击创建</router-link>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { joinCouple } from '@/api'

const router = useRouter()

const joinFormRef = ref(null)
const loading = ref(false)
const joined = ref(false)

const joinForm = reactive({
  inviteCode: ''
})

const rules = {
  inviteCode: [
    { required: true, message: '请输入邀请码', trigger: 'blur' },
    { len: 6, message: '邀请码必须是6位', trigger: 'blur' }
  ]
}

// 自动转换为大写
const handleInput = () => {
  joinForm.inviteCode = joinForm.inviteCode.toUpperCase()
}

const handleJoin = async () => {
  if (!joinFormRef.value) return

  await joinFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      try {
        await joinCouple(joinForm.inviteCode)

        // 清除"没有情侣关系"提示的标志
        sessionStorage.removeItem('hasShownNoCoupleMessage')

        joined.value = true
        ElMessage.success('成功加入情侣关系 💕')
      } catch (error) {
        console.error('加入失败：', error)
      } finally {
        loading.value = false
      }
    }
  })
}

const goHome = () => {
  router.push('/home')
}
</script>

<style scoped>
.join-couple-container {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #ffeef8 0%, #fff5f7 100%);
  padding: 20px;
}

.join-couple-card {
  width: 100%;
  max-width: 420px;
  background: #fff;
  border-radius: 24px;
  padding: 40px;
  box-shadow: 0 8px 24px rgba(255, 154, 158, 0.3);
}

.join-couple-header {
  text-align: center;
  margin-bottom: 40px;
}

.join-couple-title {
  font-size: 36px;
  font-weight: bold;
  background: linear-gradient(90deg, #ff9a9e 0%, #fecfef 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  margin-bottom: 10px;
}

.join-couple-subtitle {
  font-size: 14px;
  color: #999;
}

.join-couple-form {
  margin-top: 30px;
}

.join-button {
  width: 100%;
  background: linear-gradient(90deg, #ff9a9e 0%, #fecfef 100%);
  border: none;
  font-size: 16px;
  font-weight: bold;
}

.success-section {
  text-align: center;
  padding: 20px 0;
}

.success-icon {
  font-size: 64px;
  margin-bottom: 20px;
  animation: bounce 1s infinite;
}

@keyframes bounce {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-10px); }
}

.success-title {
  font-size: 28px;
  font-weight: bold;
  color: #ff9a9e;
  margin-bottom: 10px;
}

.success-subtitle {
  font-size: 14px;
  color: #999;
  margin-bottom: 30px;
}

.go-home-button {
  width: 100%;
}

.join-couple-footer {
  text-align: center;
  margin-top: 20px;
}

.link {
  color: #ff9a9e;
  text-decoration: none;
  font-size: 14px;
  transition: all 0.3s;
}

.link:hover {
  color: #ff8a8e;
  text-decoration: underline;
}

@media (max-width: 768px) {
  .join-couple-card {
    padding: 30px 20px;
  }

  .join-couple-title {
    font-size: 28px;
  }
}
</style>

